import { $http, $message } from '@/utils';
import { $ResponseCode } from '@/utils/const/env';
import { useUserStore } from "@/stores";
export const User = {
    userinfo: (silent: boolean) => {
        return new Promise((resolve, reject) => {
            $http.get('User/info').then((res: any) => {
                const UserStore = useUserStore();
                if (res.code === $ResponseCode.SUCCESS) {
                    UserStore.setUserInfo(res.data).then(() => resolve).catch(() => reject);
                } else if (res.code === $ResponseCode.NEED_LOGIN) {
                    UserStore.clearUserInfo().then(() => {
                        reject(res);
                    }).catch(() => {
                        reject(res);
                    });
                } else {
                    if (!silent) {
                        $message.error(res.msg);
                    }
                    reject(res);
                }
            }).catch(() => {
                reject();
                if (!silent) {
                    $message.error('获取用户信息失败');
                }
            })
        });
    },
    save(data: any) {
        return new Promise((resolve, reject) => {
            $http.post('User/save', {
                data
            }).then((res: any) => {
                if (res.code === $ResponseCode.SUCCESS) {
                    resolve(res);
                } else {
                    reject(res);
                }
            }).catch(() => {
                reject({ msg: '服务器繁忙，请稍后再试' });
            })
        })
    },
    mobile(data: any) {
        return new Promise((resolve, reject) => {
            $http.post('User/mobile', {
                data
            }).then((res: any) => {
                if (res.code === $ResponseCode.SUCCESS) {
                    resolve(res);
                } else {
                    reject(res);
                }
            }).catch(() => {
                reject({ msg: '服务器繁忙，请稍后再试' });
            })
        })
    },
    password(data: any) {
        return new Promise((resolve, reject) => {
            $http.post('User/password', {
                data
            }).then((res: any) => {
                if (res.code === $ResponseCode.SUCCESS) {
                    resolve(res);
                } else {
                    reject(res);
                }
            }).catch(() => {
                reject({ msg: '服务器繁忙，请稍后再试' });
            })
        })
    },
    statictis() {
        return new Promise((resolve, reject) => {
            $http.get('User/statictis').then((res: any) => {
                if (res.code === $ResponseCode.SUCCESS) {
                    const UserStore = useUserStore();
                    UserStore.setStatictis(res.data).catch(() => { });
                    resolve(res.data);
                } else {
                    reject();
                }
            }).catch(() => {
                reject();
            })
        })
    },
    unBindWechat() {
        return new Promise((resolve, reject) => {
            $http.get('User/unBindWechat').then((res: any) => {
                if (res.code === $ResponseCode.SUCCESS) {
                    resolve(res.data);
                } else {
                    reject(res);
                }
            }).catch(() => {
                reject({ msg: '服务器繁忙，请稍后再试' });
            })
        })
    },
    bindWechat(data: any) {
        return new Promise((resolve, reject) => {
            $http.post('User/bindWechat', {
                data
            }).then((res: any) => {
                if (res.code === $ResponseCode.SUCCESS) {
                    resolve(res);
                } else {
                    reject(res);
                }
            }).catch(() => {
                reject({ msg: '服务器繁忙，请稍后再试' });
            })
        })
    },
    newUserGift(query?: any) {
        return new Promise((resolve, reject) => {
            $http.get('User/newUserGift', {
                data: query
            }).then((res: any) => {
                if (res.code === $ResponseCode.SUCCESS) {
                    resolve(res.data);
                } else {
                    reject(res);
                }
            }).catch(() => {
                reject({ msg: '服务器繁忙，请稍后再试' });
            })
        })
    }
}